<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/5/22
 * Time: 17:28
 */

namespace App\Repositories\Eloquent;

use App\Extensions\EGrid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;

trait PolicyRepository
{
    public function grid()
    {
        return new EGrid($this->model);
    }

    /**
     * @param $user
     * @param array|int|string $id
     * @return bool
     */
    public function isOwner($user, $id)
    {
        $ids = is_array($id) ? $id : [$id];
        $objects = $this->model->find($ids);
        return $objects->filter(function ($model) use ($user) {
            if (method_exists($model, 'belong')) {
                return $model->belong->user_id != $user->id;
            }
            return $model->user_id != $user->id;
        })->isEmpty();
    }

    //用户
    public function userGrid($orderColumn = 'id', $order = 'desc')
    {
        $user = Admin::user();
        $grid = $this->grid();
        $grid->model()->where('user_id', $user->id)->orderBy($orderColumn, $order);
        return $grid;
    }

    //管理员账户
    public function adminGrid($orderColumn = 'id', $order = 'desc')
    {
        $grid = $this->grid();
        $grid->model()->orderBy($orderColumn, $order);
        return $grid;
    }

    public function deptGrid($orderColumn = 'id', $order = 'desc'): Grid
    {
        $user = Admin::user();
        $grid = $this->adminGrid($orderColumn, $order);
        $grid->model()->where('department_id', $user['department_id']);
        return $grid;
    }

    //
    public function form()
    {
        return new Form($this->model);
    }

    public function reviewCount($user, $role = 0, $review_status = [])
    {
        $review_status = is_array($review_status) ? $review_status : [$review_status];
        return $this->model->newQuery()
            ->when(!in_array($role, [1, 2]), function ($query) use ($user) {
                $query->where('user_id', $user['id']);
            })->when($role == 2, function ($query) use ($user, $review_status) {
                $query->where('department_id', $user['department_id'])->whereIn('dept_review_status', $review_status);
            })->when($role == 1, function ($query) use ($user, $review_status) {
                $query->where('dept_review_status', 1)->whereIn('review_status', $review_status);
            })->count();
    }

    public function adminReviewCount($review_status = [])
    {
        $review_status = is_array($review_status) ? $review_status : [$review_status];
        return $this->model->newQuery()->whereIn('review_status', $review_status)->count();
    }

    public function deptReviewCount($user, $review_status = [])
    {
        $review_status = is_array($review_status) ? $review_status : [$review_status];
        return $this->model->newQuery()
            ->where('department_id', $user['department_id'])
            ->where('review_status', 0)
            ->whereIn('dept_review_status', $review_status)
            ->count();
    }

    public function userWaitReviewCount($user)
    {
        return $this->model->newQuery()
            ->where('user_id', $user['id'])
            ->where('review_status', 0)
            ->count();
    }

    public function userBackReviewCount($user): int
    {
        return $this->model->newQuery()
            ->where('user_id', $user['id'])
            ->where(function ($query) {
                $query->where('review_status', 2)->orWhere('dept_review_status', 2);
            })->count();
    }

    public function needReviewCount($user, $role = 0): int
    {
        return $this->reviewCount($user, $role, 0);
    }

    public function backReviewCount($user): int
    {
        return $this->reviewCount($user, 0, 2);
    }

}
